<?php
class Ad_Movie_Episode extends CI_Controller{
    function __construct(){
        parent::__construct();

        if(!$this->session->userdata('logged_in')){
            echo "You must refresh your browser to be continute!!!";
            die;
        }
    }

    function home($movieID){
        //load episode
        $this->load->model('movie/m_movie');
        $data['episodeList'] = $this->m_movie->getEpisodeList($movieID);
        $data['movieID'] = $movieID;
        $this->load->view('manage/movie/home_episode', $data);
    }
    function addNew($movieID){
        $data['movieID'] = $movieID;
        $this->load->view('manage/movie/new_episode', $data);
    }

    function doAddNew(){
        if(!$this->input->post('new-movie')
           || !$this->input->post('txt_name')
           || !$this->input->post('id'))
            return;
        //lay post
        $name = $this->input->post('txt_name');
        $path = $this->input->post('path');
        $movieID = $this->input->post('id');
        if($path == '')
            $path = 'u add link here';
        //load model
        $this->load->model('movie/m_episode');
        $ep = new M_Episode();
        $ep->_name = $name;
        $ep->_path = $path;
        $ep->_movie_id = $movieID;
        $ep->addNew(); //add new episode

        //delete cache
        $this->db->cache_delete('movie', 'index');

        //redirect
        redirect('admincp/movie/episode/home/'. $movieID, 'location', 302);
    }

    function update($episodeID){
        //get movie_id
        $this->load->model('movie/m_episode');
        $data = array();
        $data['movie_id'] = $this->m_episode->getMovieId($episodeID);
        if(isset($_POST['update-submit'])){
            $this->_doUpdate();
            //redirect
            redirect('admincp/movie/episode/update/'.$episodeID, 'location', 302);
        }else{
            $episodeInfo = $this->m_episode->getEpisodeInfo($episodeID);
            $data['episode_name'] = $episodeInfo['episode_name'];
            $data['episode_path'] = $episodeInfo['episode_path'];
            $data['episode_id'] = $episodeID;
            $this->load->view('manage/movie/update_episode', $data);
        }
    }

    function _doUpdate(){
        $name = $this->input->post('name');
        $path = $this->input->post('path');
        $ep = new M_Episode();
        $id = $this->input->post('id');
        $ep->_episode_id = $id;
        $ep->_name = $name;
        $ep->_path = trim($path);
        //echo trim($path);
        $ep->update();
        //delete cache
        $this->db->cache_delete('video', 'urllist');
    }
}
 
